var express = require('express')
var router = express.Router()
var query = require('../db/pool').query
var queryPromise=require("../db/pool").queryPromise

router.get('/', async function (req, res, next) {
    console.log("打印session");
    console.log(req.session.user_id);
    console.log("打印session");
    var sql="select * from user_role where user_id=?"
    var userID = req.session.user_id;
    if (userID) {
        let data1 = await queryPromise(sql, userID);
        // console.log("获取roleId:",data1[0].role_id)
        var roleID = data1[0].role_id
        var sql_role = "select * from role_menu where role_id=?"
        var data2 = await queryPromise(sql_role, roleID)
        // console.log("获取到权限：", data2);
        let menus = []
        var sqlMenu="select * from menu where cat_id=?"
        for (var i = 0; i < data2.length; i++){
            var data3 = await queryPromise(sqlMenu, data2[i].cat_id)
            // console.log(data3);
            menus.push({
                cat_id: data3[0].cat_id,
                cat_name: data3[0].cat_name,
                cat_pid: data3[0].cat_pid
            })
        }
        res.json({
            code: 1, 
            data:menus,
        })
    } else {
        res.json({
            code: 0,
            msg:"请先登录！"
        })
    }

     
})

module.exports=router